extends Node2D
class_name DrawBoardV2


var move_pos:Vector2i # 移动位置
var map_size:Vector2i
var unit_size:Vector2i
var offset:Vector2i
func _ready() -> void:
	offset = BoardCommon.offset
	map_size = BoardCommon.map_size
	unit_size = BoardCommon.unit_size
	_draw_red_range()
	_draw_black_range()
	_draw_river()
	_draw_place_lines()
	

# 创建线条的辅助函数
func _create_line(start: Vector2i, end: Vector2i) -> void:
	var line = Line2D.new()
	line.width = 1
	line.default_color = Color.BLACK
	line.add_point(start)
	line.add_point(end)
	add_child(line)

## 绘制红方区域
func _draw_red_range() -> void:
	# 绘制竖线
	for i in range(0, map_size.x + 1):
		var start_point = Vector2i.RIGHT * unit_size.x * i + offset
		var end_point = start_point + Vector2i.DOWN * unit_size * 4
		_create_line(start_point, end_point)
	
	# 绘制横线
	for j in range(0, 5):
		var start_point = Vector2i.DOWN * unit_size.y * j + offset
		var end_point = start_point + Vector2i.RIGHT * unit_size * map_size.x
		_create_line(start_point, end_point)

## 绘制黑方区域
func _draw_black_range() -> void:
	# 绘制竖线
	for i in range(0, map_size.x + 1):
		var start_point = Vector2i.RIGHT * unit_size.x * i + offset
		var end_point = start_point + Vector2i.DOWN * unit_size * 4
		start_point.y += unit_size.y * 6
		end_point.y += unit_size.y * 6
		_create_line(start_point, end_point)
	
	# 绘制横线（跳过第5条横线）
	for j in range(5, map_size.y + 1):
		if j == 5: 
			continue
		var start_point = Vector2i.DOWN * unit_size.y * j + offset
		var end_point = start_point + Vector2i.RIGHT * unit_size * map_size.x
		_create_line(start_point, end_point)

## 绘制楚河汉界
func _draw_river() -> void:
	# 绘制左右边界竖线（只绘制最左边和最右边的竖线）
	for i in [0, map_size.x]:  # 只处理第0条和第8条竖线
		var start_point = Vector2i.RIGHT * unit_size.x * i + offset
		var end_point = start_point + Vector2i.DOWN * unit_size.y * map_size.y
		_create_line(start_point, end_point)
	
	# 楚河汉界文字
	var font_height = 30
	var river_label = Label.new()
	river_label.text = "楚河                           汉界"
	river_label.add_theme_font_size_override("font_size", 24)
	river_label.pivot_offset.x=river_label.size.x/2
	river_label.pivot_offset.y=river_label.size.y/2
	var river_pos = Vector2(
		offset.x + ((map_size.x - 1) * unit_size.x) / 2.0 - 100,
		offset.y + 4.5 * unit_size.y + font_height * 0.5)
	
	
	
	river_label.position = river_pos
	river_label.horizontal_alignment=HORIZONTAL_ALIGNMENT_CENTER
	
	add_child(river_label)

## 绘制斜线
func _draw_place_lines() -> void:
	# 红方九宫格对角线
	_create_line(
		Vector2i(offset.x + 3 * unit_size.x, offset.y + 8 * unit_size.y),
		Vector2i(offset.x + 5 * unit_size.x, offset.y + 10 * unit_size.y)
	)
	_create_line(
		Vector2i(offset.x + 5 * unit_size.x, offset.y + 8 * unit_size.y),
		Vector2i(offset.x + 3 * unit_size.x, offset.y + 10 * unit_size.y)
	)
	
	# 黑方九宫格对角线
	_create_line(
		Vector2i(offset.x + 3 * unit_size.x, offset.y + 0 * unit_size.y),
		Vector2i(offset.x + 5 * unit_size.x, offset.y + 2 * unit_size.y)
	)
	_create_line(
		Vector2i(offset.x + 5 * unit_size.x, offset.y + 0 * unit_size.y),
		Vector2i(offset.x + 3 * unit_size.x, offset.y + 2 * unit_size.y)
	)
